/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Component;

import GameObject.ImageObject;
import GameObject.StaticImage;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.util.ArrayList;

/**
 *
 * @author Huy
 */
public class MyForm {

	protected ArrayList<MyControlItem> items;
	protected ImageObject background;
	protected float scaleX = 1;
	protected float scaleY = 1;
	protected int width;
	protected int height;
	private Font font;

	public int getHeight() {
		return height;
	}

	public int getWidth() {
		return width;
	}

	public float getScaleX() {
		return scaleX;
	}
	public float getScaleY() {
		return scaleY;
	}
	public MyForm(BufferedImage backgroundImage, int screenWidth, int screenHeight) {
		background = new StaticImage(backgroundImage, screenWidth, screenHeight, 0, 0);
		items = new ArrayList<MyControlItem>();

		width = background.getWidth();
		height = background.getHeight();
		setSize(screenWidth, screenHeight);
		font = new Font("SansSerif", Font.BOLD, 14);
	}

	public MyForm(int formWidth, int formHeight, int screenWidth, int screenHeight) {
		items = new ArrayList<MyControlItem>();
		width = formWidth;
		height = formHeight;
		setSize(screenWidth, screenHeight);
		font = new Font("SansSerif", Font.BOLD, 14);
	}

	public void setFont(Font font) {
		this.font = font;
	}

	public final void setSize(int screenWidth, int screenHeight) {

		scaleX = (float) screenWidth / width;
		scaleY = (float) screenHeight / height;
		/*
		if (scaleX < scaleY) {
			this.scaleX = scaleX;
		} else {
			this.scaleX = scaleY;
		}*/
		for (MyControlItem myControlItem : items) {
			myControlItem.setScaleX(this.scaleX);
			myControlItem.setScaleY(this.scaleY);
		}
	}

	public void add(MyControlItem ctr) {
		ctr.setScaleX(scaleX);
		ctr.setScaleY(scaleY);
		items.add(ctr);

	}

	public void remove(MyControlItem ctr) {
		items.remove(ctr);
	}

	public void clear() {
		items.clear();
	}

	public void setForcus(MyControlItem ctr) {
		for (MyControlItem myControlItem : items) {
			if (myControlItem == ctr) {
				myControlItem.setForcus();
			} else {
				myControlItem.lostForcus();
			}
		}
	}

	public void draw(Graphics2D g) {
		g.setFont(font);
		if (background != null) {
			background.draw(g);
		}
		for (int i = items.size() - 1; i >= 0; i--) {
			items.get(i).draw(g);
		}
	}

	public void keyPressed(KeyEvent e) {
		for (MyControlItem myControlItem : items) {
			myControlItem.keyPressed(e);
		}
	}

	public void keyReleased(KeyEvent e) {
		for (MyControlItem myControlItem : items) {
			myControlItem.keyReleased(e);
		}
	}

	public void mouseDragged(MouseEvent e) {
		for (MyControlItem myControlItem : items) {
			myControlItem.mouseDragged(e);
		}
	}

	public void mouseMoved(MouseEvent e) {
		boolean flag = false;
		for (MyControlItem myControlItem : items) {
			if (flag) {
				myControlItem.mouseMoved(null);
			} else if (myControlItem.mouseMoved(e)) {
				flag = true;
			}
		}
	}

	public void mousePressed(MouseEvent e) {
		for (MyControlItem myControlItem : items) {
			if (myControlItem.mousePressed(e)) {
				return;
			}
		}
	}

	public void mouseReleased(MouseEvent e) {

		for (MyControlItem myControlItem : items) {
			if (myControlItem.mouseReleased(e)) {
				Global.GlobalGameOption.SOUND.getSound("click").start();
				//System.out.print("sound");
				return;
			}
		}
	}
}
